#install.packages("readstata13")
#install.packages("haven")
#install.packages("tidyverse")
#install.packages("xts")
#install.packages("lubridate")
#install.packages("extrafont")

library(readstata13)
library(haven)
library(tidyverse)
library(xts)
library(lubridate)
library(extrafont)
loadfonts(device = "pdf", quiet = FALSE)

#Edit the directory with the path to your folder "replication_files" 
directory = c("/replication_files")

countries = c( "A1. Enterprises - KEN1", "A2. Enterprises - SLE1", "B1. Household Expenditure - KEN1", "B2. Household Expenditure - SLE1", "C1. Prices - KEN1", "C2. Prices - SLE1", "D1. Food Insecurity - KEN1", "D2. Food Insecurity - SLE1", "E1. Domestic Violence - KEN1" )
colors <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

pdf(paste0(directory[1],"/results/WeeklyPanels.pdf"), height = 16, width = 9)
df = read.dta13(paste0(directory[1],"/data/forgraphs_KEN1.dta"))
d_sl = read.dta13(paste0(directory[1],"/data/forgraphs_SLE1.dta"))
d_sl_ent = read.dta13(paste0(directory[1], "/data/forgraphs_ent_SLE1.dta"))
rev_prof = read.dta13(paste0(directory[1], "/data/forgraphs_KEN1.dta"))


par(mfrow = c(5,2))
par(family="Helvetica")

for(i in 1){
  thisvar1="profits_wins" #profits
  thisvar2="rev_wins" #revenues
  thisvar3="profits_wins2" #dummy_profits
  thisvar4="rev_wins2" #dummy_revenues
  thisvar="rev_wins"
  
  plot(0,0, type = "n", ylim = c(-100, 50), xlim = range(df$week, df$week), axes = F, xlab = ifelse(i == 9, "Week", ""), ylab = ifelse(i == 1, "% change", ""), main = countries[i], cex.lab=1.4, cex.main=1.8)
  
  axis(1, at = df$week[1:nrow(df)], labels = c("Pre" , df$week[2:nrow(df)]))
  mtext(c("February"),at=-0.75,side=1,line=3,cex=0.75)  
  axis(2, at = NULL , las = 1)
  mtext(c("Mar 22"),at=1,side=1,line=3,cex=0.75)  
  mtext(c("May 31"),at=11,side=1,line=3,cex=0.75)  
  mtext(c("COVID-19"),at=0.5,side=1,line=-16,cex=0.8)
  
  points(range(df$week), c(0, 0), lty = 3, lwd = 0.4, type = "l")
  
  ## demarcate lockdown
  abline(v = 0.5, lty = 2)
  
  lowbar1 = sum((rev_prof[7:nrow(rev_prof), "week_nent"]) * (rev_prof[7:nrow(rev_prof), thisvar1])) / sum((rev_prof[7:nrow(rev_prof), "week_nent"]))
  p1 = rev_prof[1, paste0(thisvar1, "_p")]
  
  percchange1 = round(lowbar1, 0)
  
  lowbar2 = sum((rev_prof[7:nrow(rev_prof), "week_nent"]) * (rev_prof[7:nrow(rev_prof), thisvar2])) / sum((rev_prof[7:nrow(rev_prof), "week_nent"]))
  p2 = rev_prof[1, paste0(thisvar2, "_p")]
  
  percchange2 = round(lowbar2, 0)
  max1=round(max(abs((df[2:nrow(df), thisvar1]))),0)
  max2=round(max(abs((df[2:nrow(df), thisvar2]))),0)
  ## plot the data
  points(rev_prof$week, rev_prof[,thisvar1], type = "l", lwd = 5, col = colors[5])
  points(rev_prof$week, rev_prof[,thisvar3], type = "l", lwd = 5, col = colors[5], lty= "dotted")
  points(rev_prof$week, rev_prof[,thisvar2], type = "l", lwd = 5, col = colors[6], lty= "dashed")
  points(rev_prof$week, rev_prof[,thisvar4], type = "l", lwd = 5, col = colors[6], lty= "dotted")
  points(0, 0, pch = 19, col = colors[5], cex = 2)
  legend("topright", col = c(colors[5], colors[6]), legend = c(paste0("a. Profits. Avg. Change = ", percchange1, "%*"), paste0("b. Revenues. Avg. Change = ", percchange2, "%*")), lty = c("solid", "dashed") , bty ="n" , lwd = 3, cex=1.2)
  
}

for(i in 2){
  thisvar1="enterprise_profits" #profits
  thisvar2="enterprise_profits2" #dummy_profits
  plot(0,0, type = "n", ylim = c(-100,50), xlim = range(d_sl_ent$week, d_sl_ent$week), axes = F, xlab = ifelse(i == 9, "Week", ""), ylab = ifelse(i == 3,  "% change", ""), main = countries[i], cex.lab=1.4, cex.main=1.8)
  
  ## axis(1, at = d_sl_ent$week[1:nrow(d_sl_ent)], labels = c("Pre" , d_sl_ent$week[2:10]-1))
  axis(1, at = d_sl$week[1:nrow(d_sl)], labels = c("Pre" , d_sl$week[2:nrow(d_sl)]-1))
  axis(2, at = NULL , las = 1)
  mtext(c("March"),at=0.5,side=1,line=3,cex=0.75)  
  mtext(c("Apr 19"),at=2,side=1,line=3,cex=0.75)  
  mtext(c("June 28"),at=12,side=1,line=3,cex=0.75) 
  points(range(d_sl_ent$week), c(0, 0), lty = 3, lwd = 0.4, type = "l")
  
  ## demarcate lockdown
  abline(v = 1.5, lty = 2)
  
  ##lowbar1 = sum((d_sl_ent[2:10, "enterprise_profits_n"]) * (d_sl_ent[2:10, thisvar1])) / sum((d_sl_ent[2:10, "enterprise_profits_n"]))
  lowbar1 = sum((d_sl_ent[4:nrow(d_sl), "enterprise_profits_n"]) * (d_sl_ent[4:nrow(d_sl), thisvar1])) / sum((d_sl_ent[4:nrow(d_sl), "enterprise_profits_n"]))
  p1 = d_sl_ent[1, paste0(thisvar1, "_p")]
  
  stars1 = ifelse(p1 > 0.05, "", 
                  ifelse(p1 <= 0.05, "*"))
  
  percchange1 = round(lowbar1, 0)
  max1=round(max(abs((d_sl[2:nrow(d_sl), thisvar1]))),0)
  ## plot the data
  points(d_sl_ent$week, d_sl_ent[,thisvar1], type = "l", lwd = 5, col = colors[5])
  points(d_sl_ent$week, d_sl_ent[,thisvar2], type = "l", lwd = 5, col = colors[5], lty= "dotted")
  points(1, 0, pch = 19, col = colors[5], cex = 2)
  legend("topright", col = c(colors[5]), legend = c(paste0("a. Profits. Avg. Change = ", percchange1, "%", stars1)), lty = c("solid") , bty ="n" , lwd = 3, cex=1.2)
  
}

for(i in 3){
  thisvar1="c2_wins"
  thisvar2="c3_wins"
  thisvar="c3_wins"
  
  plot(0,0, type = "n", ylim = c(-60,40), xlim = range(df$week, df$week), axes = F, xlab = ifelse(i == 9, "Week", ""), ylab = ifelse(i == 3, "% change", ""), main = countries[i], cex.lab=1.4, cex.main=1.8)
  axis(1, at = df$week[1:nrow(df)] , labels = c("Pre" , df$week[2:nrow(df)]))
  axis(2, at = NULL , las = 1)
  mtext(c("Mar 22"),at=1,side=1,line=3,cex=0.75)  
  mtext(c("May 31"),at=11,side=1,line=3,cex=0.75)  
  
  points(range(df$week), c(0, 0), lty = 3, lwd = 0.4, type = "l")

  ## demarcate lockdown
  abline(v = 0.5, lty = 2)
  
  lowbar1 = sum((df[3:nrow(d_sl), "c2_wins_n"]) * (df[3:nrow(d_sl), thisvar1])) / sum((df[3:nrow(d_sl), "c2_wins_n"]))
  p1 = df[2, paste0(thisvar1, "_p")]
  
  stars1 = ifelse(p1 > 0.05, "", 
                  ifelse(p1 <= 0.05, "*"))
  
  percchange1 = round(lowbar1, 0)
  
  lowbar2 = sum((df[3:nrow(d_sl), "c3_wins_n"]) * (df[3:nrow(d_sl), thisvar2])) / sum((df[3:nrow(d_sl), "c3_wins_n"]))
  p2 = df[2, paste0(thisvar2, "_p")]
  
  stars2 = ifelse(p2 > 0.05, "", 
                  ifelse(p2 <= 0.05, "*"))
  
  percchange2 = round(lowbar2, 0)
 # max1=round(max(abs((df[2:nrow(df), thisvar1]))),0)
#  max2=round(max(abs((df[2:nrow(df), thisvar2]))),0)
  
  ## plot the data
  points(df$week, df[,thisvar1], type = "l", lwd = 5, col = colors[1])
  points(df$week, df[,thisvar2], type = "l", lwd = 5, col = colors[2], lty = "dashed")
  points(1, 0, pch = 19, col = colors[1], cex = 2)
  legend("topright", col = c(colors[1], colors[2]), legend = c(paste0("a. Food expenditure. Avg. Change = ", percchange1, "%", stars1), paste0("b. Non-food expenditure. Avg. Change = ", percchange2, "%", stars2)), lty = c("solid", "dashed") , bty ="n", lwd = 3, cex=1.2)
  
}

for(i in 4){
  thisvar1="food_consumption"
  thisvar="food_consumption"
  
  plot(0,0, type = "n", ylim = c(-60,40), xlim = range(d_sl$week, d_sl$week), axes = F, xlab = ifelse(i == 9, "Week", ""), ylab = ifelse(i == 3, "% change", ""), main = countries[i], cex.lab=1.4, cex.main=1.8)
  
  axis(1, at = d_sl$week[1:nrow(d_sl)], labels = c("Pre" , d_sl$week[2:nrow(d_sl)]-1))
  axis(2, at = NULL , las = 1)
  mtext(c("March"),at=0.5,side=1,line=3,cex=0.75)  
  mtext(c("Apr 19"),at=2,side=1,line=3,cex=0.75)  
  mtext(c("June 28"),at=12,side=1,line=3,cex=0.75) 
  points(range(d_sl$week), c(0, 0), lty = 3, lwd = 0.4, type = "l")
  
  ## demarcate lockdown
  abline(v = 1.5, lty = 2)
  
  lowbar1 = sum((d_sl[2:nrow(d_sl), "food_consumption_n"]) * (d_sl[2:nrow(d_sl), thisvar1])) / sum((d_sl[2:nrow(d_sl), "food_consumption_n"]))
  p1 = d_sl[1, paste0(thisvar1, "_p")]
  
  stars1 = ifelse(p1 > 0.05, "", 
                  ifelse(p1 <= 0.05, "*"))
  
  percchange1 = round(lowbar1, 0)
  max1=round(max(abs((d_sl[2:nrow(d_sl), thisvar1]))),0)
  ## plot the data
  points(d_sl$week, d_sl[,thisvar1], type = "l", lwd = 5, col = colors[1])
  points(1, 0, pch = 19, col = colors[1], cex = 2)
  legend("topright", col = c(colors[1]), legend = c(paste0("a. Food expenditure. Avg. Change = ", percchange1, "%", stars1)), lty = c("solid") , bty ="n", lwd = 3, cex=1.2)
  
}

for(i in 5){
  thisvar1="foodpriceindex"
  thisvar2="nonfoodpriceindex"
  thisvar="foodpriceindex"
  plot(0,0, type = "n", ylim = c(-40,40), xlim = range(df$week, df$week), axes = F, xlab = ifelse(i == 9, "Week", ""), ylab = ifelse(i == 5, "% change", ""), main = countries[i], cex.lab=1.4, cex.main=1.8)
  
  axis(1, at = df$week[1:nrow(df)] , labels = c("Pre" , df$week[2:nrow(df)]))
  axis(2, at = NULL , las = 1)
  mtext(c("Mar 22"),at=1,side=1,line=3,cex=0.75)  
  mtext(c("May 31"),at=11,side=1,line=3,cex=0.75)  
  
  points(range(df$week), c(0, 0), lty = 3, lwd = 0.4, type = "l")
  
  ## demarcate lockdown
  abline(v = 0.5, lty = 2)
  
  lowbar1 = sum((df[5:nrow(d_sl), "week_n"]) * (df[5:nrow(d_sl), thisvar1])) / sum((df[5:nrow(d_sl), "week_n"]))
  p1 = df[5, paste0(thisvar1, "_p")]
  
  stars1 = ifelse(p1 > 0.05, "", 
                  ifelse(p1 <= 0.05, "*"))
  
  percchange1 = round(lowbar1, 0)
  
  lowbar2 = sum((df[5:nrow(d_sl), "week_n"]) * (df[5:nrow(d_sl), thisvar2])) / sum((df[5:nrow(d_sl), "week_n"]))
  p2 = df[5, paste0(thisvar2, "_p")]
  
  stars2 = ifelse(p2 > 0.05, "", 
                  ifelse(p2 <= 0.05, "*"))
  
  percchange2 = round(lowbar2, 0)
  max1=round(max(abs((df[2:nrow(df), thisvar1]))),0)
  max2=round(max(abs((df[2:nrow(df), thisvar2]))),0)
   ## plot the data
  points(df$week, df[,thisvar1], type = "l", lwd = 5, col = colors[3])
  points(df$week, df[,thisvar2], type = "l", lwd = 5, col = colors[4], lty = "dashed")
  points(3, 0, pch = 19, col = colors[3], cex = 2)
  legend("topright", col = c(colors[3], colors[4]), legend = c(paste0("a. Food prices. Avg. Change = ", percchange1, "%", stars1), paste0("b. Non-food prices. Avg. Change = ", percchange2, "%", stars2)), lty = c("solid", "dashed") , bty ="n", lwd = 3, cex=1.2)

  }
for(i in 6){
  thisvar1="food_prices"
  thisvar="food_prices"
  plot(0,0, type = "n", ylim = c(-40,40), xlim = range(d_sl$week, d_sl$week), axes = F, xlab = ifelse(i == 9, "Week", ""), ylab = ifelse(i == 5, "% change", ""), main = countries[i], cex.lab=1.4, cex.main=1.8)
  
  axis(1, at = d_sl$week[1:nrow(d_sl)], labels = c("Pre" , d_sl$week[2:nrow(d_sl)]-1))
  axis(2, at = NULL , las = 1)
  mtext(c("March"),at=0.5,side=1,line=3,cex=0.75)  
  mtext(c("Apr 19"),at=2,side=1,line=3,cex=0.75)  
  mtext(c("June 28"),at=12,side=1,line=3,cex=0.75) 
  points(range(d_sl$week), c(0, 0), lty = 3, lwd = 0.4, type = "l")
  
  ## demarcate lockdown
  abline(v = 1.5, lty = 2)
  
  lowbar1 = sum((d_sl[2:nrow(d_sl), "food_prices_n"]) * (d_sl[2:nrow(d_sl), thisvar1])) / sum((d_sl[2:nrow(d_sl), "food_prices_n"]))
  p1 = d_sl[1, paste0(thisvar1, "_p")]
  
  stars1 = ifelse(p1 > 0.05, "", 
                  ifelse(p1 <= 0.05, "*"))
  
  percchange1 = round(lowbar1, 0)
  max1=round(max(abs((d_sl[2:nrow(d_sl), thisvar1]))),0)
  ## plot the data
  points(d_sl$week, d_sl[,thisvar1], type = "l", lwd = 5, col = colors[3])
  points(1, 0, pch = 19, col = colors[3], cex = 2)
  legend("topright", col = c(colors[3]), legend = c(paste0("a. Food prices. Avg. Change = ", percchange1, "%", stars1)), lty = c("solid") , bty ="n", lwd = 3, cex=1.2)
  
}

for(i in 7){
  thisvar1="f1_d_skippedadult_wins"
  thisvar2="f1_d_skippedchild_wins"
  thisvar="f1_d_skippedadult_wins"
  
  plot(0,0, type = "n", ylim = c(-25, 150), xlim = range(df$week, df$week), axes = F, xlab = ifelse(i == 9, "Week", ""), ylab = ifelse(i == 7, "% change", ""), main = countries[i], cex.lab=1.4, cex.main=1.8)
  
  axis(1, at = df$week[1:nrow(df)] , labels = c("Pre" , df$week[2:nrow(df)]))
  axis(2, at = NULL , las = 1)
  mtext(c("Mar 22"),at=1,side=1,line=3,cex=0.75)  
  mtext(c("May 31"),at=11,side=1,line=3,cex=0.75)  
  
  points(range(df$week), c(0, 0), lty = 3, lwd = 0.4, type = "l")
  
  
  ## demarcate lockdown
  abline(v = 0.5, lty = 2)
  
  lowbar1 = sum((df[4:nrow(d_sl), "f1_d_skippedadult_wins_n"]) * (df[4:nrow(d_sl), thisvar1])) / sum((df[4:nrow(d_sl), "f1_d_skippedadult_wins_n"]))
  p1 = df[3, paste0(thisvar1, "_p")]
  
  stars1 = ifelse(p1 > 0.05, "", 
                  ifelse(p1 <= 0.05, "*"))
  
  percchange1 = round(lowbar1, 0)
  
  lowbar2 = sum((df[4:nrow(d_sl), "f1_d_skippedchild_wins_n"]) * (df[4:nrow(d_sl), thisvar2])) / sum((df[4:nrow(d_sl), "f1_d_skippedchild_wins_n"]))
  p2 = df[3, paste0(thisvar2, "_p")]
  
  stars2 = ifelse(p2 > 0.05, "", 
                  ifelse(p2 <= 0.05, "*"))
  
  percchange2 = round(lowbar2, 0)
  max1=round(max(abs((df[2:nrow(df), thisvar1]))),0)
  max2=round(max(abs((df[2:nrow(df), thisvar2]))),0)
  ## plot the data
  points(df$week, df[,thisvar1], type = "l", lwd = 5, col = colors[7])
  points(df$week, df[,thisvar2], type = "l", lwd = 5, col = colors[8], lty="dashed")
  points(2, 0, pch = 19, col = colors[7], cex = 2)
  legend("topright", col = c(colors[7], colors[8]), legend = c(paste0("a. Adults skipped meals. Avg. Change = ", percchange1, "%", stars1), paste0("b. Children skipped meals. Avg. Change = ", percchange2, "%", stars2)), lty = c("solid", "dashed") , bty ="n" , lwd = 3, cex=1.2)
  
  
}

for(i in 8){
  thisvar1="adult_skipped_meals"
  thisvar2="child_skipped_meals"
  thisvar="adult_skipped_meals"
  
  plot(0,0, type = "n", ylim = c(-25, 150), xlim = range(d_sl$week, d_sl$week), axes = F, xlab = ifelse(i == 7, "Week", ""), ylab = ifelse(i == 7, "% change", ""), main = countries[i], cex.lab=1.4, cex.main=1.8)
  
  axis(1, at = d_sl$week[1:nrow(d_sl)], labels = c("Pre" , d_sl$week[2:nrow(d_sl)]-1))
  axis(2, at = NULL , las = 1)
  mtext(c("2019"),at=0.5,side=1,line=3,cex=0.75)  
  mtext(c("Apr 19"),at=2,side=1,line=3,cex=0.75)  
  mtext(c("June 28"),at=12,side=1,line=3,cex=0.75) 
  points(range(d_sl$week), c(0, 0), lty = 3, lwd = 0.4, type = "l")
  
  ## demarcate lockdown
  abline(v = 1.5, lty = 2)
  
  lowbar1 = sum((d_sl[2:nrow(d_sl), "adult_skipped_meals_n"]) * (d_sl[2:nrow(d_sl), thisvar1])) / sum((d_sl[2:nrow(d_sl), "adult_skipped_meals_n"]))
  p1 = d_sl[1, paste0(thisvar1, "_p")]
  
  stars1 = ifelse(p1 > 0.05, "", 
                  ifelse(p1 <= 0.05, "*"))
  
  percchange1 = round(lowbar1, 0)
  
  lowbar2 = sum((d_sl[2:nrow(d_sl), "child_skipped_meals_n"]) * (d_sl[2:nrow(d_sl), thisvar2])) / sum((d_sl[2:nrow(d_sl), "child_skipped_meals_n"]))
  p2 = d_sl[1, paste0(thisvar2, "_p")]
  
  stars2 = ifelse(p2 > 0.05, "", 
                  ifelse(p2 <= 0.05, "*"))
  
  percchange2 = round(lowbar2, 0)
  max1=round(max(abs((d_sl[2:nrow(d_sl), thisvar1]))),0)
  max2=round(max(abs((d_sl[2:nrow(d_sl), thisvar2]))),0)
  ## plot the data
  points(d_sl$week, d_sl[,thisvar1], type = "l", lwd = 5, col = colors[7])
  points(d_sl$week, d_sl[,thisvar2], type = "l", lwd = 5, col = colors[8], lty="dashed")
  points(1, 0, pch = 19, col = colors[7], cex = 2)
  legend("topright", col = c(colors[7], colors[8]), legend = c(paste0("a. Adults reducing portions. Avg. Change = ", percchange1, "%", stars1), paste0("b. Children reducing portions. Avg. Change = ", percchange2, "%", stars2)), lty = c("solid", "dashed") , bty ="n" , lwd = 3, cex=1.2)
  
}

for(i in 9){
  thisvar1="genderviolence" 
  thisvar2="childrenbeaten"
  thisvar="genderviolence"
  
  ##plot(0,0, type = "n", ylim = c(round(min(d_sl[,thisvar]), -1)-20,round(max(d_sl[,thisvar]), -1)+20), xlim = range(d_sl$week, d_sl$week), axes = F, xlab = ifelse(i == 9, "Week", ""), ylab = ifelse(i == 9, "% change", ""), main = countries[i])
  plot(0,0, type = "n", ylim = c(-40,80), xlim = range(df$week, df$week), axes = F, xlab = ifelse(i == 9, "Week", ""), ylab = ifelse(i == 9, "% change", ""), main = countries[i], cex.lab=1.4, cex.main=1.8)
  
  
  
  axis(1, at = df$week[1:nrow(df)] , labels = c("Pre" , df$week[2:nrow(df)]))
  axis(2, at = NULL , las = 1)
  mtext(c("Mar 22"),at=1,side=1,line=3,cex=0.75)  
  mtext(c("May 31"),at=11,side=1,line=3,cex=0.75)  
  
  points(range(df$week), c(0, 0), lty = 3, lwd = 0.4, type = "l")
  
  ## demarcate lockdown
  abline(v = 0.5, lty = 2)
  
  lowbar1 = sum((df[3:nrow(df), "genderviolence_n"]) * (df[3:nrow(df), thisvar1])) / sum((df[3:nrow(df), "genderviolence_n"]))
  p1 = df[2, paste0(thisvar1, "_p")]
  
  stars1 = ifelse(p1 > 0.05, "", 
                  ifelse(p1 <= 0.05, "*"))
  
  percchange1 = round(lowbar1, 0)
  
  lowbar2 = sum((df[3:nrow(df), "childrenbeaten_n"]) * (df[3:nrow(df), thisvar2])) / sum((df[3:nrow(df), "childrenbeaten_n"]))
  p2 = df[2, paste0(thisvar2, "_p")]
  
  stars2 = ifelse(p2 > 0.05, "", 
                  ifelse(p2 <= 0.05, "*"))
  
  percchange2 = round(lowbar2, 0)
  max1=round(max(abs((df[2:nrow(df), thisvar1]))),0)
  max2=round(max(abs((df[2:nrow(df), thisvar2]))),0)
  
  ## plot the data
  points(df$week, df[,thisvar1], type = "l", lwd = 5, col = "deeppink4")
  points(df$week, df[,thisvar2], type = "l", lwd = 5, col = "dodgerblue4", lty = "dashed")
  points(1, 0, pch = 19, col = "deeppink4", cex = 2)
  legend("topright", col = c("deeppink4", "dodgerblue4"), legend = c(paste0("a. Adults domestic violence. Avg. Change = ", percchange1, "%", stars1), paste0("b. Children domestic violence. Avg. Change = ", percchange2, "%", stars2)), lty = c("solid", "dashed") , bty ="n" , lwd = 3, cex=1.2)
  
}

dev.off()
embed_fonts(paste0(directory[1],"/results/WeeklyPanels.pdf"), outfile=(paste0(directory[1],"/results/WeeklyPanels.pdf")))
